<?php

namespace App\Http\Controllers\home\user;

use App\Models\Coupon;
use App\Org\wxpay\WXTool;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\User_info;
use App\Models\Address;
use App\Models\Order;

class UserController extends Controller
{
    //个人中心主页
    public function index()
    {
        $id = session('homeuser');
        $user = new User();
        $ob = $user->sel()->where('user.id', $id)->first();
        $order = new Order();
        $row = $order->userorder();
        $status = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0];
        if ($row) {
            foreach ($row as $v) {
                switch ($v->status) {
                    case '1':
                        $status[1] += 1;
                        break;
                    case '2':
                        $status[2] += 1;
                        break;
                    case '3':
                        $status[3] += 1;
                        break;
                    case '4':
                        $status[4] += 1;
                        break;
                    case '5':
                        $status[5] += 1;
                        break;
                    case '6':
                        $status[5] += 1;
                        break;
                }
            }
        } else {
            $status = null;
        }
        return view('home.user.index', ['list' => $ob, 'status' => $status]);
    }

    //遍历地址
    public function dizhi()
    {
        $id = session('homeuser');
        $address = new Address();
        $row = $address->addselect($id);
        return view('home.user.dizhi', ['list' => $row]);
    }

    //修改地址信息
    public function updadizhi($id)
    {
        $address = new Address();
        $row = $address->addfirst($id);
        return view('home.user.updadizhi', ['list' => $row]);
    }

    //删除地址
    public function deldizhi($id)
    {
        $address = new Address();
        $address->adddelete($id);
        return redirect('/home/user/dizhi');
    }

    //跳转至添加地址页面
    public function adddizhi()
    {
        return view('home.user.adddizhi');
    }

    public function pwd()
    {
        return view('home.user.pwd');
    }

    //遍历个人信息
    public function userinfo()
    {

        $id = session('homeuser');
        $user = new User();
        $ob = $user->sel()->where('user.id', $id)->first();
        return view('home.user.userinfo', ['list' => $ob]);
    }

    //修改个人信息
    public function userupdate(Request $request, $id)
    {
        $messages = [
//            'name.regex'=>'真实姓名必须为汉字',
            'sex.regex' => '请填写年龄',
            'age.regex' => '年龄必须在3个数字以内',

        ];
        $this->validate($request, [
//            'name' => 'bail|regex:/^[\u4E00-\u9FA5]{1,6}$/',
            'sex' => 'bail|regex:/[mw]/',
            'age' => 'bail|regex:/^[0-9]{1,3}$/',
        ], $messages);

        $user_info = new User_info();
        $row = $user_info->userUpdate($request, $id);
        if ($row > 0) {
            return redirect('/home/user/userinfo')->with('msg', '修改信息成功');
        } else {
            return redirect()->back();
        }

    }

    public function register($id = 0)
    {
        //dd($id);
        return view('home.user.register', ['pid' => $id]);
    }

    public function login()
    {
        return view('home.user.login');
    }

    //微信登录
    public function wxlogin(Request $request, $pid = 0)
    {

        //$pid = $request->input('pid');
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {

        } else {
            $code = $request->input('code', '');
            if ($code == '') {
                // 指定微信重定向后的地址
                $redirect_uri = urlencode('http://' . $_SERVER['HTTP_HOST'] . '/home/user/wxlogin/' . $pid);
                // 微信重定向
                $url = 'https://open.weixin.qq.com/connect/oauth2/authorize' .
                    '?appid=' . config('wxpay.APPID') .
                    '&redirect_uri=' . $redirect_uri .
                    '&response_type=code' .
                    '&scope=snsapi_userinfo' .
                    '&state=STATE' .
                    '#wechat_redirect';
                return redirect($url);
            }
            //dd($code);
            $userinfo = WXTool::getUserinfo($code);
            //var_dump($userinfo);
            //检查user里是否已存在该微信用户
            $res = User::where('openid', $userinfo->openid)->first();

            if ($res) {
                session(['homeuser' => $res->id]);
            } else {
                $res = new User;
                $res->username = $userinfo->openid;
                $res->password = $userinfo->openid;
                $res->openid = $userinfo->openid;
                $res->save();
                $request->session()->flash('msg', '');
                if ($pid != '') {
                    $res->pid = $pid;
                    //给推荐人发优惠券
                    $coupon = new Coupon();
                    $coupon->user_id = $pid;
                    $coupon->value = 10;
                    $coupon->status = 1;
                    $coupon->info = '满40元可用';
                    $coupon->start = date(time());
                    $coupon->end = date(time() + 2592000);
                    $coupon->threshold = 40;
                    $res->save();
                    $coupon->save();
                    //给注册者发优惠券
                    $coupon_u = new Coupon();
                    $coupon_u->user_id = $res->id;
                    $coupon_u->value = 10;
                    $coupon_u->status = 1;
                    $coupon_u->info = '满40元可用';
                    $coupon_u->start = date(time());
                    $coupon_u->end = date(time() + 2592000);
                    $coupon_u->threshold = 40;
                    $coupon_u->save();
                    $request->session()->flash('msg', '恭喜您获得10元优惠券，马上去购物吧！');
                }
                $user_info = new User_info();
                $user_info->user_id = $res->id;
                $user_info->sex = $userinfo->sex == 1 ? 'm' : 'w';
                $user_info->nickname = $userinfo->nickname;
                $user_info->photo = $userinfo->headimgurl;
                $user_info->save();
                session(['homeuser' => $res->id]);
            }
        }
        return redirect('home/user');
    }
}
